home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3.iso / chapte21 / aviedit.c < prev    next >
C/C++ Source or Header  |  1996-04-29  |  11KB  |  403 lines

  1.  
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include "AviEdit.h"
  5. #include <vfw.h>
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18.  
  19.  
  20. HINSTANCE hInst;   // current instance
  21.  
  22. LPCTSTR lpszAppName = "MyApp";
  23. LPCTSTR lpszTitle   = "My Application"; 
  24.  
  25. // the rest of the stuff
  26. //......................
  27.  
  28. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  29.  
  30.  
  31. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  32.                       LPTSTR lpCmdLine, int nCmdShow)
  33. {
  34.    MSG      msg;
  35.    HWND     hWnd; 
  36.    WNDCLASS wc;
  37.  
  38.    wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  39.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  40.    wc.cbClsExtra    = 0;                      
  41.    wc.cbWndExtra    = 0;                      
  42.    wc.hInstance     = hInstance;              
  43.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  44.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  45.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  46.    wc.lpszMenuName  = lpszAppName;              
  47.    wc.lpszClassName = lpszAppName;              
  48.  
  49.    if ( IS_WIN95 )
  50.    {
  51.       if ( !RegisterWin95( &wc ) )
  52.          return( FALSE );
  53.    }
  54.    else if ( !RegisterClass( &wc ) )
  55.       return( FALSE );
  56.  
  57.    hInst = hInstance; 
  58.  
  59.    hWnd = CreateWindow( lpszAppName, 
  60.                         lpszTitle,    
  61.                         WS_OVERLAPPEDWINDOW, 
  62.                         CW_USEDEFAULT, 0, 
  63.                         CW_USEDEFAULT, 0,  
  64.                         NULL,              
  65.                         NULL,              
  66.                         hInstance,         
  67.                         NULL               
  68.                       );
  69.  
  70.    if ( !hWnd ) 
  71.       return( FALSE );
  72.  
  73.    ShowWindow( hWnd, nCmdShow ); 
  74.    UpdateWindow( hWnd );         
  75.  
  76.    while( GetMessage( &msg, NULL, 0, 0) )   
  77.    {
  78.       TranslateMessage( &msg ); 
  79.       DispatchMessage( &msg );  
  80.    }
  81.  
  82.    return( msg.wParam ); 
  83. }
  84.  
  85.  
  86. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  87. {
  88.     WNDCLASSEX wcex;
  89.  
  90.    wcex.style         = lpwc->style;
  91.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  92.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  93.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  94.    wcex.hInstance     = lpwc->hInstance;
  95.    wcex.hIcon         = lpwc->hIcon;
  96.    wcex.hCursor       = lpwc->hCursor;
  97.    wcex.hbrBackground = lpwc->hbrBackground;
  98.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  99.    wcex.lpszClassName = lpwc->lpszClassName;
  100.  
  101.    // Added elements for Windows 95.
  102.    //...............................
  103.    wcex.cbSize = sizeof(WNDCLASSEX);
  104.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  105.                             IMAGE_ICON, 16, 16,
  106.                             LR_DEFAULTCOLOR );
  107.             
  108.    return RegisterClassEx( &wcex );
  109. }
  110.  
  111.  
  112. LRESULT CALLBACK About( HWND hDlg,           
  113.                         UINT message,        
  114.                         WPARAM wParam,       
  115.                         LPARAM lParam)
  116. {
  117.    switch (message) 
  118.    {
  119.        case WM_INITDIALOG: 
  120.                return (TRUE);
  121.  
  122.        case WM_COMMAND:                              
  123.                if (   LOWORD(wParam) == IDOK         
  124.                    || LOWORD(wParam) == IDCANCEL)    
  125.                {
  126.                        EndDialog(hDlg, TRUE);        
  127.                        return (TRUE);
  128.                }
  129.                break;
  130.    }
  131.  
  132.    return (FALSE); 
  133. }
  134.  
  135.  
  136. #define MSG_LEN          1024
  137.  
  138. char msg[MSG_LEN+1];
  139.  
  140. VOID OpenAvi(HWND hWnd);
  141. VOID EditAvi(HWND hWnd);
  142. VOID CloseAvi();
  143.  
  144.  
  145. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  146. {
  147.    switch( uMsg )
  148.    {
  149.       case WM_CREATE :
  150.               // initialize the AVI library DLL
  151.               //...............................
  152.  
  153.               AVIFileInit();
  154.               break;
  155.  
  156.       case WM_COMMAND :
  157.               switch( LOWORD(wParam) )
  158.               {
  159.                  case IDM_TEST:
  160.                         OpenAvi(hWnd);
  161.                         EditAvi(hWnd);
  162.                         CloseAvi();
  163.                         break;
  164.  
  165.                  case IDM_ABOUT :
  166.                         DialogBox( hInst, "AboutBox", hWnd, About );
  167.                         break;
  168.  
  169.                  case IDM_EXIT :
  170.                         DestroyWindow( hWnd );
  171.                         break;
  172.               }
  173.               break;
  174.  
  175.       case WM_DESTROY :
  176.               // release the AVI library DLL
  177.               //............................
  178.  
  179.               AVIFileExit();
  180.  
  181.               PostQuitMessage(0);
  182.               break;
  183.  
  184.       default :
  185.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  186.    }
  187.  
  188.    return(0L);               
  189. }
  190.  
  191. /*****************************************************************************
  192. *  Global variables
  193. *
  194. *
  195. *
  196. ******************************************************************************/
  197.  
  198. HRESULT    rc;
  199.  
  200. LPCTSTR    lpszInputFilename = "small.avi";
  201.  
  202. PAVIFILE   pAviIn            = NULL;
  203. PAVIFILE   pAviOut           = NULL;
  204. PAVISTREAM pAudioStream      = NULL;
  205. PAVISTREAM pVideoStream      = NULL;
  206.  
  207. /*****************************************************************************
  208. *  OpenAvi
  209. *
  210. *
  211. *
  212. ******************************************************************************/
  213.  
  214. VOID OpenAvi(HWND hWnd)
  215. {
  216.    LONG          i;
  217.    AVIFILEINFO   pFileInfo;
  218.    PAVISTREAM    pAviStream;
  219.    AVISTREAMINFO StreamInfo;
  220.  
  221.    // open input AVI file
  222.    //....................
  223.  
  224.    rc = AVIFileOpen(&pAviIn, lpszInputFilename, OF_READ, NULL);
  225.  
  226.    if (rc) // if error
  227.    {
  228.        MessageBox(hWnd, "Error opening AVI file.",
  229.                   NULL, MB_OK);
  230.        return;
  231.    }
  232.    
  233.    // get input AVI file info
  234.    //........................
  235.  
  236.    pFileInfo.dwStreams = 0L;
  237.  
  238.    rc = AVIFileInfo(pAviIn, &pFileInfo, sizeof(AVIFILEINFO));
  239.  
  240.    // locate AVI streams
  241.    //...................
  242.  
  243.    for (i = 0; i < (LONG)pFileInfo.dwStreams; i++) 
  244.    { 
  245.       rc = AVIFileGetStream(pAviIn, &pAviStream, 0L, i);
  246.        
  247.       if (rc) 
  248.         break;
  249.  
  250.       rc = AVIStreamInfo(pAviStream, &StreamInfo, sizeof(AVISTREAMINFO)); 
  251.  
  252.       if (rc)
  253.           continue;
  254.  
  255.       if (StreamInfo.fccType == streamtypeVIDEO)
  256.       {
  257.           pVideoStream = pAviStream;
  258.           AVIStreamAddRef(pVideoStream);
  259.       }
  260.       else if (StreamInfo.fccType == streamtypeAUDIO)
  261.       {
  262.           pAudioStream = pAviStream;
  263.           AVIStreamAddRef(pAudioStream);
  264.       }
  265.     }
  266. }
  267.  
  268. /*****************************************************************************
  269. *  EditAvi
  270. *
  271. *
  272. *
  273. ******************************************************************************/
  274.  
  275. VOID EditAvi(HWND hWnd)
  276. {
  277.    LONG                 i;
  278.    PAVISTREAM           pEditAudioStream;
  279.    PAVISTREAM           pEditVideoStream;
  280.    PAVISTREAM           pCloneAudioStream;
  281.    PAVISTREAM           pReverseVideoStream;
  282.    PAVISTREAM           pCutVideoStream;
  283.    AVISTREAMINFO        StreamInfo;
  284.    
  285.    PAVISTREAM           pSaveStream[2];
  286.    AVICOMPRESSOPTIONS   SaveOptions[2];
  287.    LPAVICOMPRESSOPTIONS pSaveOptions[2];
  288.  
  289.    LONG       nPos, nLen;
  290.  
  291.    // create editable audio/video streams
  292.    //....................................
  293.  
  294.    CreateEditableStream(&pEditAudioStream, pAudioStream);
  295.    CreateEditableStream(&pEditVideoStream, pVideoStream);
  296.  
  297.    // clone the audio stream
  298.    //.......................
  299.  
  300.    EditStreamClone(pEditAudioStream, &pCloneAudioStream);
  301.  
  302.    if ( !AVIStreamInfo(pCloneAudioStream, &StreamInfo, sizeof(AVISTREAMINFO)) )
  303.    {
  304.        strcpy(StreamInfo.szName, "Cloned audio stream.");
  305.        EditStreamSetInfo(pCloneAudioStream, &StreamInfo, sizeof(AVISTREAMINFO));
  306.    }
  307.  
  308.    // copy the video stream
  309.    //......................
  310.  
  311.    nPos = AVIStreamStart(pEditVideoStream);
  312.    nLen = AVIStreamLength(pEditVideoStream);
  313.  
  314.    EditStreamCopy(pEditVideoStream, &nPos, &nLen,
  315.                   &pReverseVideoStream);
  316.  
  317.    // cut the the back half of the copied stream 
  318.    // and paste in at the beginning
  319.    // therefore flipping the first half of the video 
  320.    // with the second half.
  321.    //...............................................
  322.  
  323.    nPos = nLen / 2;
  324.    nLen -= nPos;
  325.  
  326.    EditStreamCut(pReverseVideoStream, &nPos, &nLen,
  327.                  &pCutVideoStream);
  328.  
  329.    nPos = AVIStreamStart(pEditVideoStream);
  330.  
  331.    EditStreamPaste(pReverseVideoStream, &nPos, &nLen,
  332.                    pCutVideoStream, 0, -1);
  333.  
  334.    EditStreamSetName(pReverseVideoStream, "1/2 reversed video stream.");
  335.  
  336.    // create a new AVI file from the newly created streams
  337.    //.....................................................
  338.  
  339.    pSaveStream[0] = pCloneAudioStream;
  340.    pSaveStream[1] = pReverseVideoStream;
  341.  
  342.    // get save (compression) options
  343.    //...............................
  344.  
  345.    for (i = 0; i < 2; i++)
  346.    {
  347.       // AVISaveOptions takes an array of pointers to our 
  348.       // compression options (cleared)
  349.  
  350.       pSaveOptions[i] = &SaveOptions[i];
  351.       memset(pSaveOptions[i], 0, sizeof(AVICOMPRESSOPTIONS));
  352.    }
  353.  
  354.    AVISaveOptions(hWnd,
  355.                   ICMF_CHOOSE_KEYFRAME | ICMF_CHOOSE_DATARATE | 
  356.                   ICMF_CHOOSE_PREVIEW,
  357.                   2,
  358.                   pSaveStream,
  359.                   pSaveOptions);
  360.  
  361.    remove("AviEdit.avi"); // destroy previous version (if any)
  362.    
  363.    AVISave("AviEdit.avi", NULL, NULL, 2,
  364.            pSaveStream[0], pSaveOptions[0],
  365.            pSaveStream[1], pSaveOptions[1]);
  366.  
  367.    // free save options resourses
  368.    //............................
  369.  
  370.    AVISaveOptionsFree(2, pSaveOptions);
  371.  
  372.    AVIStreamRelease(pEditAudioStream);
  373.    AVIStreamRelease(pEditVideoStream);
  374.    AVIStreamRelease(pCloneAudioStream);
  375.    AVIStreamRelease(pReverseVideoStream);
  376.    AVIStreamRelease(pCutVideoStream);
  377. }
  378.  
  379. /*****************************************************************************
  380. *  CloseAvi
  381. *
  382. *
  383. *
  384. ******************************************************************************/
  385.  
  386. VOID CloseAvi()
  387. {
  388.    // release AVI streams
  389.    //....................
  390.  
  391.    if (pVideoStream)
  392.        AVIStreamRelease(pVideoStream);
  393.  
  394.    if (pAudioStream)
  395.        AVIStreamRelease(pAudioStream);
  396.  
  397.    // close AVI files
  398.    //................
  399.  
  400.    AVIFileRelease(pAviIn);
  401. }
  402.  
  403.